package common;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;


public class Utils {
    public static WebDriver driver=null;

    public static WebDriver createDirver(String url){
        //1、驱动程序管理地的自动化
        WebDriverManager.chromedriver().setup();
        //2、创建驱动对象
        ChromeOptions options=new ChromeOptions();
        //3、增加浏览器配置：允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
        //设置为无头模式
        //options.addArguments("-headless");
        //4、打开浏览器
        driver=new ChromeDriver(options);
        //5、进入网址
        driver.get(url);
        //6、隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        return driver;
    }

    //屏幕截图
    public void getScreenShot(String str) throws IOException {
        SimpleDateFormat sim1=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2=new SimpleDateFormat("HHmmssSS");

        String dirTime=sim1.format(System.currentTimeMillis());
        String fileTime=sim2.format(System.currentTimeMillis());

        // ./src/test/image/2024-07-17/LoginTest-174530.png
        String filename="./src/test/image"+dirTime+"/"+str+"-"+fileTime+".png";

        //截图并存放到指定位置
        File srcfile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcfile,new File(filename));
    }

}
